#ifdef WITH_OPENGL
#include <algorithm>
#include "driver_opengl.h"
#include "renderer_opengl.h"
#include "jobs.h"
#include <setjmp.h>
#include <cassert>

#ifdef _WIN32
#include <windows.h>
#include "renderer_opengl_win32.h"
#endif

using namespace krryn;
using namespace krryn::system;
using namespace krryn::system_imp;

namespace krryn{
namespace system_imp{
	static struct opengl_factory : system::driver_factory{
		opengl_factory(){
			driver::attach("opengl", this);
		}

		virtual driver* create(){
			return new driver_opengl();
		}

	} _opengl;
}
}

driver_opengl::driver_opengl(){
}

driver_opengl::~driver_opengl(){
}

renderer *driver_opengl::create_renderer(krryn::core::job_manager *a_Manager, const window_settings &a_Settings) {
#ifdef _WIN32
	// Notice that we renderer_opengl_win32
	// doesn't inherit from renderer_opengl but
	// directly from renderer. The reason is simple:
	// this allows us to swap in a new opengl renderer
	// without having to modify the platform specific
	// class.
	//
	// This is nice for in the future for when we,
	// for example, would like a renderer with
	// multiple render stages. Or one that lets
	// use run a post processing filter.
	return new system_imp::renderer_opengl_win32(
		new system_imp::renderer_opengl(a_Manager, a_Settings.m_Viewport), 
		a_Manager, 
		a_Settings
	);
#else
	throw invalid_input_exception("Platform not yet supported");
#endif
}

#endif
